﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinCourseControls
{
    /// <summary>
    /// 判断点与多边形位置关系
    /// </summary>
    public class PolygonPointRelation
    {
        public Point[] Points;

        public PolygonPointRelation(Point[] points)
        {
            Points = points;
        }

        public PolygonEdgeCrossing GetPointRelation(Point point)
        {
            int count = 0;
            for (int i = 0; i < Points.Length; i++)
            {
                if (Points[i].X < point.X && Points[i + 1].X > point.X)
                {
                    if (Points[i].Y < point.Y && Points[i + 1].Y > point.Y)
                    {
                        count++;
                    }
                }
            }
            if (count % 2 == 1) return PolygonEdgeCrossing.Inside;
            else if (count % 2 == 0) return PolygonEdgeCrossing.Outside;
            else return PolygonEdgeCrossing.OnEdge;
        }
    }
}
